﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileConverter
{
    class Converter
    {
        private StreamReader _sr;
        private List<DEntry> _dlist;
        public Converter(string path)
        {
            _sr = new StreamReader(new FileStream(path, FileMode.Open));
            _dlist = new List<DEntry>();
            
        }

        public void Convert()
        {
            string[] spstr = _sr.ReadToEnd().Split('_');
            foreach (string s in spstr)
            {
                string[] sub = s.Split('^');
                DEntry de = new DEntry() { Title = sub[0] };
                try
                {
                    for (int i = 1; i < sub.Length; i++)
                    {
                        HandleSubcases(sub[i], de);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                _dlist.Add(de);
            }
            WriteOut();
        }

        private void WriteOut()
        {
            StreamWriter str = new StreamWriter(new FileStream("C:\\Users\\Ayush\\Documents\\fxf\\result.txt", FileMode.Create));
            foreach (DEntry de in _dlist)
            {
                str.Write(de.GenString());
            }
        }

        private void HandleSubcases(string m, DEntry d)
        {
            string[] x = m.Split(':');
            switch (x[0])
            {
                case "Author":
                    d.Author = x[1];
                    break;
                case "Publication info":
                    d.Publication = x[1];
                    break;
                case "Abstract":
                    d.Abstract = x[1];
                    break;
                case "Subject":
                    d.Subject = x[1];
                    break;
                case "Classification":
                    d.Classification = x[1];
                    break;
                case "Identifier / keyword":
                    d.Identifier = x[1];
                    break;
                case "Number of pages":
                    d.NumberOfPages = x[1];
                    break;
                case "Publication year":
                    d.PublicationYear = x[1];
                    break;
                case "Degree date":
                    d.DegreeDate = x[1];
                    break;
                case "Section":
                    d.Section = x[1];
                    break;
                case "Source":
                    d.Source = x[1];
                    break;
                case "ISBN":
                    d.ISBN = x[1];
                    break;
                case "Advisor":
                    d.Advisor = x[1];
                    break;
                case "Committee member":
                    d.CommitteeMember = x[1];
                    break;
                case "University/institution":
                    d.UniversityInstitution = x[1];
                    break;
                case "Department":
                    d.Department = x[1];
                    break;
                case "University location":
                    d.UniversityLocation = x[1];
                    break;
                case "Degree":
                    d.Degree = x[1];
                    break;
                case "Source type":
                    d.SourceType = x[1];
                    break;
                case "Language":
                    d.Language = x[1];
                    break;
                case "Document type":
                    d.DocumentType = x[1];
                    break;
                case "Dissertation/thesis number":
                    d.Dissertation = x[1];
                    break;
                case "ProQuest document ID":
                    d.ProQuestDocID = x[1];
                    break;
                case "Document URL":
                    d.URL = joinUrl(x);
                    break;
                case "Copyright":
                    d.Copyright = x[1];
                    break;
                case "Database":
                    d.Database = x[1];
                    break;
                case "Pages":
                    d.Database = x[1];
                    break;
                default:
                    d.misc = x[0];
                    break;
            }
            
        }

        private string joinUrl(string[] x)
        {
            string s = "";
            for (int i = 1; i < x.Length; i++) s += x[i];
            return s;
        }
    }
}
